package www.wuhen.ltd.User;


import okhttp3.*;

import java.io.IOException;
import java.util.Random;


/**
 * @author wuhen
 * 白鲸加速器刷时长
 */
public class Invitation {
    // 邀请码
    private static final String BJCODE = "JMAK7";

    // 邀请的人数，12小时内同一ip不建议超过20
    private static final int TIMES = 1;

    // 主程序方法
    public static void main(String[] args) {
        // 邀请人数计数
        int invitedCount = 0;
        for (int i = 0; i < TIMES; i++) {
            try {
                invite();
                invitedCount++;
                System.out.println("验证码：" + BJCODE + " 已邀请" + invitedCount + "个人");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void invite() throws IOException {
        // 随机手机
        String[] phoneNames = {"oppo-pedm00", "oppo-peem00", "oppo-peam00", "oppo-x907", "oppo-x909t",
                "vivo-v2048a", "vivo-v2072a", "vivo-v2080a", "vivo-v2031ea", "vivo-v2055a",
                "huawei-tet-an00", "huawei-ana-al00", "huawei-ang-an00", "huawei-brq-an00", "huawei-jsc-an00",
                "xiaomi-mi 10s", "xiaomi-redmi k40 pro+", "xiaomi-redmi k45", "xiaomi-redmi k60", "xiaomi-mi 11",
                "xiaomi-mi 6", "xiaomi-redmi note 7", "xiaomi-redmi k70 pro+", "xiaomi-mi 14",
                "meizu-meizu 18", "meizu-meizu 18 pro", "meizu-mx2", "meizu-m355", "meizu-16th plus",
                "samsung-sm-g9910", "samsung-sm-g9960", "samsung-sm-w2021", "samsung-sm-f7070", "samsung-sm-c7000",
                "oneplus-le2120", "oneplus-le2110", "oneplus-kb2000", "oneplus-hd1910", "oneplus-oneplus a3010",
                "sony-xq-as72", "sony-f8132", "sony-f5321", "sony-i4293", "sony-g8231",
                "google-pixel", "google-pixel xl", "google-pixel 2", "google-pixel 2 xl", "google-pixel 3"};
        String phone = phoneNames[new Random().nextInt(phoneNames.length)];

        // 邮箱的实现
        String email = generateRandomEmail();

        String data = "passwd=a5fc859e9c4a1a6c5e7edeab9cc0debc&email=" + email + "&invite_code=" + BJCODE;

        // 获取时间戳
        String t = String.valueOf(System.currentTimeMillis());

        // 随机获取id
        String id = generateRandomId();

        // 拼接请求地址
        String url = "https://sm01.googls.net/account/register?" +
                "platform=2&api_version=14&" +
                "app_version=1.44&lang=zh&_key=&" +
                "market_id=1000&" +
                "pkg=com.bjchuhai&" +
                "device_id=rk_" + id + "&" +
                "model=" + phone + "&" +
                "sys_version=7.1.2&" +
                "ts=" + t + "&" +
                "sub_pkg=com.bjchuhai&" +
                "version_code=44";

        OkHttpClient client = new OkHttpClient();
        // 请求参数body
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), data);
        // 请求参数header
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Content-Length", String.valueOf(data.length()))
                .addHeader("Host", "sm01.google.net")
                .addHeader("Connection", "Keep-Alive")
                .addHeader("Accept-Encoding", "gzip")
                .addHeader("User-Agent", "okhttp/3.5.0")
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 获取响应体的内容
            //String responseBody = response.body().string();
            //System.out.println("Response: " + responseBody);
        }
    }

    // 生成随机邮箱方法
    private static String generateRandomEmail() {
        StringBuilder email = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            email.append(random.nextInt(10));
        }
        StringBuilder xx = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            xx.append((char) (random.nextInt(26) + 'A'));
        }
        email.append("%40").append(xx).append(".com");
        return email.toString();
    }

    // 生成随机id
    private static String generateRandomId() {
        String characters = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder id = new StringBuilder();
        Random random = new Random();
        // 最大id长度
        int code = 32;
        for (int i = 0; i < code; i++) {
            id.append(characters.charAt(random.nextInt(characters.length())));
        }
        return id.toString();
    }
}
